<template>
  <div class="app-container">
    <h1>资源归还</h1>
    <el-table :data="claimedResources" style="width: 100%">
      <el-table-column prop="name" label="资源名称"></el-table-column>
      <el-table-column prop="claimedQuantity" label="已领用量"></el-table-column>
      <el-table-column fixed="right" label="操作" width="120">
        <template slot-scope="scope">
          <el-button size="small" @click="handleReturn(scope.$index, scope.row)">归还</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog title="归还资源" :visible.sync="dialogVisible" width="30%">
      <el-form :model="returnForm" label-width="100px">
        <el-form-item label="资源名称">
          <el-input v-model="returnForm.name" disabled></el-input>
        </el-form-item>
        <el-form-item label="归还量">
          <el-input-number v-model="returnForm.returnedQuantity" :min="1"
            :max="returnForm.claimedQuantity"></el-input-number>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="confirmReturn">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      claimedResources: [
        { id: 1, name: '农药A', claimedQuantity: 5 },
        { id: 2, name: '喷雾器', claimedQuantity: 1 },
        { id: 3, name: '除草机', claimedQuantity: 2 }
      ],
      dialogVisible: false,
      returnForm: {
        id: null,
        name: '',
        returnedQuantity: 0,
        claimedQuantity: 0
      }
    };
  },
  methods: {
    handleReturn(index, row) {
      this.returnForm.id = row.id;
      this.returnForm.name = row.name;
      this.returnForm.returnedQuantity = 0;
      this.returnForm.claimedQuantity = row.claimedQuantity;
      this.dialogVisible = true;
    },
    confirmReturn() {
      const resourceIndex = this.claimedResources.findIndex(resource => resource.id === this.returnForm.id);
      if (resourceIndex !== -1) {
        this.claimedResources[resourceIndex].claimedQuantity -= this.returnForm.returnedQuantity;
        // 这里可以添加实际的归还逻辑，比如更新库存等
      }
      this.dialogVisible = false;
      console.log('资源已归还:', this.returnForm);
    }
  }
};
</script>

<style scoped>
.app-container {
  padding: 20px;
}
</style>



